// pages/jifen/jifen.js
var app = getApp()
var uid = ''
var uname=''
var uface=''
var upoints=''
var balance=0

// 请求数据

var url = app.globalData.url+"v1/goods/";

var loadMore = function(that){
    that.setData({
        hidden:false
    });
    wx.request({
        url:url,
        method: 'GET',
        data:{
        },
        success:function(res){
          console.log(res)
            var list = that.data.list;
            var inumber =that.data.inumber
            for(var i = 0; i < res.data.goods.length; i++){
                list.push(res.data.goods[i]);
                inumber.push(res.data.goods[i].number);
            }
            that.setData({
                list : list
            });
            that.setData({
                hidden:true
            });
        }
    });
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    hidden:true,
    list:[],
    inumber:[],
    scrollTop : 0,
    scrollHeight:0,
    //***************************************** */
    // points:10000
  },
   
  btn:function (e) {
    console.log(this.data.inumber)
    var that = this;
    balance = this.data.points-e.target.dataset.point;

    var iid = e.target.dataset.id;
    var image = e.target.dataset.image;
    var name = e.target.dataset.name;
    var point = e.target.dataset.point;
    var number = e.target.dataset.number;
    var inumber = this.data.inumber;
    var index = "inumber(" + e.currentTarget.dataset.index + ")";
    // var dnumber = this.data.inumber.indexOf(e.target.dataset.inedx)-1 //---------
    // console.log(inumber);
    if((balance)<0 && e.target.dataset.number>0){
      wx.showToast({
      title: '您的积分不足！',
      icon: 'none',
      duration: 1500
    })
    }else{  
      if(e.target.dataset.number>0) {
        that.setData({
            points:balance, //*******************修改积分
            [index]:number-1//--------
        })
        //*******************修改数据库积分
        wx.request({
          url:  app.globalData.url + 'v1/user/update',
          method: 'POST',
          data: {
            id:uid,
            name:uname,
            face:uface,
            points:balance
          },    
          success (res) {
            console.log(res)
          }
        })
        //*******************修改库存 */
        wx.request({
          url: app.globalData.url + 'v1/goods/update',
          method: 'POST',
          data: {
              id:iid,
              image:image,
              name:name,
              point:point,
              number:number-1              
          },   
          success (res) {
            console.log(res)
          }
        })
        wx.showToast({
          title: '恭喜你兑换成功！',
          icon: 'none',
          duration: 1500
        })
        // this.data.number=e.target.dataset.number-1
      }else{
        wx.showToast({
          title: '库存不足，正在通知相关人员紧急加货....-(>_<)-',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
  
  choujiang:function(e){
    wx.navigateTo({
      url: '../choujiang/choujiang',
    })
   
  },

 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;

        wx.getSystemInfo({
            success:function(res){
                  that.setData({
                    scrollHeight:res.windowHeight
                });
              }
          });
        loadMore(that);
       
  },

    //页面滑动到底部
  bindDownLoad:function(){   
        var that = this;
        loadMore(that);
        console.log("lower");
  },
  scroll:function(event){
      //该方法绑定了页面滚动时的事件，记录当前的position.y的值,为了请求数据之后把页面定位到这里。
       this.setData({
           scrollTop : event.detail.scrollTop
       });
  },

  topLoad:function(event){
      // 该方法绑定了页面滑动到顶部的事件，然后做上拉刷新
        this.setData({
            list : [],
            scrollTop : 0
        });
        loadMore(this);
        console.log("upper");
      
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that =this;
    wx.request({
      url: app.globalData.url + 'v1/user/'+app.globalData.userInfo.id,  
      method: 'GET',
      success (res) {
        console.log(res)
        that.setData({
          id: res.data.user.id,
          name: res.data.user.name,
          face: res.data.user.face,
          points: res.data.user.points
          //*********************
        })
        uid=that.data.id,
        uname = that.data.name,
        uface = that.data.face,
        upoints = that.data.points
        //******** */
        
        // if(upoints==undefined){
        //   that.setData({
        //     upoints:0
        //   })
        // }
      }
    })
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})